package com.ptma.android.client;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

public class ApkReader {
	private AaptLoader aaptLoader = new AaptLoader();

	public static void main(String[] args) throws Exception {
		Apk apk = new ApkReader().getApk(new File(
				"C:/Users/yarshad/Downloads/2.apk"));
		System.out.println(apk.toString());
	}

	public Apk getApk(File apkFile) throws Exception {
		Apk apk = new Apk();

		String aaptFile = aaptLoader.getPlatformAapt(OS.getOS());
		String ls_str;
		if (OS.getOS() == OS.LINUX || OS.getOS() == OS.MAC) {
			Process chmodProcess = Runtime.getRuntime().exec(
					"chmod 777 " + aaptFile);
			 
		} else {
//			Process chmodProcess = Runtime.getRuntime().exec(
//					"chmod 777 " + aaptFile);
//			chmodProcess.wait();
		}

		Process ls_proc = Runtime.getRuntime().exec(
				aaptFile + " dump badging " + apkFile.getAbsolutePath());

		BufferedReader input = new BufferedReader(new InputStreamReader(
				ls_proc.getInputStream()));

		while ((ls_str = input.readLine()) != null) {

			// Packatge Name
			if (ls_str.contains("package: name='")) {
				String pkg = null;
				int start = ls_str.indexOf("package: name='");
				if (start > -1) {
					start += +"package: name='".length();
					int end = ls_str.indexOf("'", start);
					pkg = ls_str.substring(start, end).trim();
				}
				apk.setPackageName(pkg);
			}

			// Version Code
			if (ls_str.contains("versionCode='")) {
				String versionCode = null;
				int start = ls_str.indexOf("versionCode='");
				if (start > -1) {
					start += +"versionCode='".length();
					int end = ls_str.indexOf("'", start);
					versionCode = ls_str.substring(start, end).trim();
				}
				apk.setVersionCode(versionCode);
			}

			// Version Name
			if (ls_str.contains("versionName='")) {
				String versionName = null;
				int start = ls_str.indexOf("versionName='");
				if (start > -1) {
					start += +"versionName='".length();
					int end = ls_str.indexOf("'", start);
					versionName = ls_str.substring(start, end).trim();
				}
				apk.setVersionName(versionName);
			}

			// Application Label
			if (ls_str.contains("application-label:'")) {
				String appLabel = null;
				int start = ls_str.indexOf("application-label:'");
				if (start > -1) {
					start += +"application-label:'".length();
					int end = ls_str.indexOf("'", start);
					appLabel = ls_str.substring(start, end).trim();
				}
				apk.setApplicationLabel(appLabel);
			}

			if (ls_str.startsWith(apk.getIconFolderName())) {
				String appIcon = null;
				int start = ls_str.indexOf("application-icon-120:");
				if (start > -1) {
					start += +"application-icon-120:".length() + 1;
					int end = ls_str.indexOf("'", start);
					appIcon = ls_str.substring(start, end).trim();
				}

				JarFile jf = new JarFile(apkFile.getAbsolutePath());
				ZipEntry ze = jf.getEntry(appIcon);
				InputStream in = jf.getInputStream(ze);
				byte[] buffer = new byte[512];
				int len = in.read(buffer);
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				while (len != -1) {
					baos.write(buffer, 0, len);
					len = in.read(buffer);
					if (Thread.interrupted()) {
						throw new InterruptedException();
					}
				}
				apk.setApkIcon(baos.toByteArray());

			}

		}
		return apk;
	}

}
